// pages/ShopIndex/shopping/shopping.js
const ShopHttp = require('../../../utils/ShopHttp');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        carts: [],
        allChecked: true,
        count: 0
    },
    onChange(e) {
        var { aid } = e.currentTarget.dataset;
        var carts = this.data.carts;
        carts.forEach(item => {
            item.collect = item._id == aid ? e.detail : item.collect;
        })
        var allChecked = carts.every(item => {
            return item.collect
        })
        this.handleCount();
        this.setData({
            carts,
            allChecked
        })
    },
    onAllChange(e) {
        var carts = this.data.carts;
        carts.forEach(item => {
            item.collect = e.detail
        })
        this.handleCount();
        this.setData({
            carts,
            allChecked: e.detail
        })
    },
    handleNumber(e) {
        var { aid } = e.currentTarget.dataset;
        var carts = this.data.carts;
        carts.forEach(item => {
            item.productCount = item._id == aid ? e.detail : item.productCount;
        })
        this.handleCount();
        wx.setStorageSync('carts', carts);
        this.setData({
            carts
        })
    },
    handleCount() {
        var count = 0;
        var carts = this.data.carts;
        carts.forEach(item => {
            if (item.collect) {
                count += item.productCount * item.price
            }
        })
        count = Math.round(count * 100) / 100
        this.setData({
            count
        })
    },
    handleDelete(e) {
        var { aid } = e.currentTarget.dataset;
        var carts = this.data.carts;
        carts.forEach(item => {
            if (item._id == aid) {
                wx.request({
                    url: 'http://122.112.142.6:8888/api/kindCollect',
                    data: {
                        id: aid,
                        collect: false
                    },
                    header: {
                        "Content-Type": "application/x-www-form-urlencoded"
                    },
                    method: 'POST',
                    success: async (res) => {
                        if (res.data.msg == "取消收藏") {
                            wx.showToast({
                                title: '删除成功 !',
                                icon: 'none',
                            })
                            //如果删除成功再次请求数据
                            var res = await ShopHttp.getUserCollect();
                            var carts = wx.getStorageSync('carts');
                            res.data.res.forEach(item => {
                                if (carts) {
                                    carts.forEach(cart => {
                                        if (item._id == cart._id) {
                                            item.productCount = cart.productCount;
                                        }
                                    })
                                } else {
                                    item.productCount = 1
                                }
                            })
                            this.setData({
                                carts: res.data.res
                            })
                            this.handleCount();
                            var shopNum = this.data.carts.length;
                            wx.setTabBarBadge({
                                index: 2,
                                text: String(shopNum),
                            })
                            var allChecked = this.data.allChecked;
                            if (shopNum == 0) {
                                allChecked = false
                            } else {
                                allChecked = true
                            }
                            this.setData({
                                allChecked
                            })
                        }
                    },
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: async function (options) {
        var res = await ShopHttp.getUserCollect();
        this.setData({
            carts: res.data.res
        })
        this.handleCount();
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: async function () {
        var res = await ShopHttp.getUserCollect();
        var carts = wx.getStorageSync('carts');
        res.data.res.forEach(item => {
            if (carts) {
                carts.forEach(cart => {
                    if (item._id == cart._id) {
                        if (cart.productCount) {
                            item.productCount = cart.productCount;
                        } else {
                            item.productCount = 1
                        }
                    }
                })
            } else {
                item.productCount = 1
            }
        })
        this.setData({
            carts: res.data.res
        })
        this.handleCount();
        var shopNum = this.data.carts.length;
        var allChecked = this.data.allChecked;
        if (shopNum == 0) {
            allChecked = false
        } else {
            allChecked = true
        }
        wx.setTabBarBadge({
            index: 2,
            text: String(shopNum),
        })
        this.setData({
            allChecked
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
        var carts = this.data.carts;
        wx.setStorageSync('carts', carts);
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    // onPullDownRefresh: async function () {
    //     var res = await ShopHttp.getUserCollect();
    //     var carts = wx.getStorageSync('carts');
    //     res.data.res.forEach(item => {
    //         if (carts) {
    //             carts.forEach(cart => {
    //                 if (item._id == cart._id) {
    //                     if (cart.productCount) {
    //                         item.productCount = cart.productCount;
    //                     } else {
    //                         item.productCount = 1
    //                     }
    //                 }
    //             })
    //         } else {
    //             item.productCount = 1
    //         }
    //     })
    //     this.setData({
    //         carts: res.data.res
    //     })
    //     this.handleCount();
    // },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        wx.clearStorageSync();
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})